﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using Sirenix.OdinInspector;
using UnityEngine.UI;

namespace Miao.UI
{
    /// <summary>
    /// 可在游戏特定位置，开启一个对话框
    /// </summary>
    [SourcesPath("Assets/Plugins/Miao/UIPrefab/RoleDialogue.prefab"), UILayer(0, Layer.WindowLayer)]
    public class RoleDialog : UIMotionPanel
    {
        public RectTransform RolePanel;
        public RectTransform HeadTransform;
        public RectTransform DialogTransform;

        [Title("对象设置")]
        public Image roleHead;
        public TextMeshProUGUI roleName;
        public Typewriter typewriter;
        [Tooltip("进行文本回调")]
        public Button NextButton;
        [Title("播放设置")]
        public bool autoPlay;
        [Tooltip("需要播放的文本")]
        public string[] textQuantity;
        public int textPtr { get; set; }
        bool handClick;
        public bool playing;
        public Action OnPlayCompleted;

        private void Reset()
        {
            RolePanel = transform as RectTransform;
            HeadTransform = transform.Find("HeadTransform") as RectTransform;
            DialogTransform = transform.Find("DialogTransform") as RectTransform;
            roleHead =  HeadTransform.Find("Head").GetComponent<Image>();
            roleName = HeadTransform.Find("Name").GetComponent<TextMeshProUGUI>();
        }
        public override void OnShow()
        {
            base.OnShow();
            NextButton.onClick.AddListener(NextEvent);
            Play();
        }
        public override void OnClose()
        {
            base.OnClose();
            NextButton.onClick.RemoveListener(NextEvent);
        }

        public void Play()
        {
            if (handClick) return;
            if (textPtr < textQuantity.Length)
            {
                playing = true;
                if (autoPlay)
                {
                    OnPlay();
                    typewriter.PlayText(textQuantity[textPtr], Play);
                }
                else
                {
                    OnPlay();
                    typewriter.PlayText(textQuantity[textPtr], null);
                }
                textPtr++;
            }
            else
            {
                playing = false;
            }
        }
        virtual public void OnPlay()
        {

        }

        /// <summary>
        /// 手动呼叫
        /// </summary>
        public void NextEvent()
        {
            if (typewriter.State == TypewriterState.Outputting)
            {
                handClick = true;
                typewriter.CompleteOutput();
            }
            else
            {
                handClick = false;
                Play();
            }
            if(!playing)
            {
                OnPlayCompleted.Invoke();
            }

        }

        //private void Update()
        //{
        //    if(Input.GetKeyDown(KeyCode.A))
        //    {
        //        typewriter.OutputText("Content1234567899999999999999999");
        //    }
        //    if (Input.GetKeyDown(KeyCode.D))
        //    {
        //        typewriter.OutputText("345675676");
        //    }
        //    if (Input.GetKeyDown(KeyCode.S))
        //    {
        //        typewriter.OutputText("这是普通话");
        //    }
        //}

        public void SetTopLeft()
        {
            RolePanel.TopLeft();
            HeadTransform.TopLeft();
            DialogTransform.TopRight();
        }
        public void SetTop()
        {
            RolePanel.Top();
        }
        public void SetTopRight()
        {
            RolePanel.TopRight();
            HeadTransform.TopRight();
            DialogTransform.TopLeft();
        }
        public void SetLeft()
        {
            RolePanel.Left();
            HeadTransform.TopLeft();
            DialogTransform.TopRight();
        }
        public void SetRight()
        {
            RolePanel.Right();
            HeadTransform.TopRight();
            DialogTransform.TopLeft();
        }
        public void SetBottomLeft()
        {
            RolePanel.BottomLeft();
            HeadTransform.TopLeft();
            DialogTransform.TopRight();
        }
        public void SetBottom()
        {
            RolePanel.Bottom();
        }

        public void SetBottomRight()
        {
            RolePanel.BottomRight();
            HeadTransform.TopRight();
            DialogTransform.TopLeft();
        }
    }
}